package com.smile.stream.group;

import com.smile.stream.StreamUtil;
import com.smile.stream.pojo.Dish;
import com.smile.stream.pojo.Type;

import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.*;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/10/7 21:29
 */
public class GroupDemo2 {
    public static void main(String[] args) {
        List<Dish> menu = StreamUtil.getMenu();
        Map<Type, Map<CaloricLevel, List<Dish>>> dishesByTypeCaloricLevel = menu
                .stream()
                .collect(groupingBy(Dish::getType,
                        groupingBy(dish -> {
                            if (dish.getCalories() <= 400) {
                                return CaloricLevel.DIET;
                            } else if (dish.getCalories() <= 700) {
                                return CaloricLevel.NORMAL;
                            } else {
                                return CaloricLevel.FAT;
                            }
                        })));
        dishesByTypeCaloricLevel.entrySet().forEach(System.out::println);
        /**
         * 输出结果
         * FISH={NORMAL=[salmon], DIET=[prawns]}
         * MEAT={NORMAL=[beef], DIET=[chicken], FAT=[pork]}
         * OTHER={NORMAL=[french fries, pizza], DIET=[rice, season fruit]}
         */
    }

    public enum CaloricLevel {DIET,NORMAL,FAT}
}
